package com.hunliji.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.hunliji.common.utils.StringUtil;
import com.hunliji.monitor.dao.mapper.mt.ChatGroupMapper;
import com.hunliji.monitor.dao.model.dataobject.AlertChatBindDO;
import com.hunliji.monitor.dao.model.dataobject.AlertConfigDO;
import com.hunliji.monitor.dao.model.dataobject.ChatGroupDO;
import com.hunliji.monitor.dao.model.param.QueryChatGroupDataParam;
import com.hunliji.monitor.service.api.ChatGroupService;
import com.hunliji.monitor.service.api.model.bo.ChatGroupBO;
import com.hunliji.monitor.service.api.model.param.QueryChatGroupParam;
import com.hunliji.monitor.share.component.ComponentLocator;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import org.springframework.util.CollectionUtils;

/**
 * 和群组有关的
 * @author wuchen
 */
@Service
public class ChatGroupServiceImpl implements ChatGroupService {
    @Resource
    private ChatGroupMapper chatGroupMapper;
    @Override
    public void save(ChatGroupBO chatGroupBO) {
        ChatGroupDO chatGroupDO = ComponentLocator.getConverter().convert(chatGroupBO,ChatGroupDO.class);
        chatGroupMapper.insert(chatGroupDO);
    }

    @Override
    public void update(ChatGroupBO chatGroupBO) {
        ChatGroupDO chatGroupDO = ComponentLocator.getConverter().convert(chatGroupBO,ChatGroupDO.class);
        chatGroupMapper.updateById(chatGroupDO);
    }

    @Override
    public List<ChatGroupBO> query(QueryChatGroupParam queryChatGroupParam) {
        LambdaQueryWrapper<ChatGroupDO> queryWrapper = new LambdaQueryWrapper<ChatGroupDO>();
        if(Objects.nonNull(queryChatGroupParam.getFeiShuChatId())){
            queryWrapper.eq(ChatGroupDO::getFeiShuChatId, queryChatGroupParam.getFeiShuChatId());
        }
        if(StringUtil.isNotBlank(queryChatGroupParam.getName())){
            queryWrapper.eq(ChatGroupDO::getName, queryChatGroupParam.getName());
        }
        queryWrapper.eq(ChatGroupDO::getDeleted, Boolean.FALSE);

        List<ChatGroupDO> chatGroupDOList = chatGroupMapper.selectList(queryWrapper);
        List<ChatGroupBO> chatGroupBOList = ComponentLocator.getConverter().convertList(chatGroupDOList,ChatGroupBO.class);
        return chatGroupBOList;
    }

    @Override
    public Map<Long, ChatGroupBO> getMapByIds(List<Long> chatGroupIds) {
        LambdaQueryWrapper<ChatGroupDO> queryWrapper = new LambdaQueryWrapper<ChatGroupDO>()
            .in(CollectionUtils.isEmpty(chatGroupIds), ChatGroupDO::getId, chatGroupIds)
            .eq(ChatGroupDO::getDeleted, Boolean.FALSE);
        List<ChatGroupDO> chatGroupDOList = chatGroupMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(chatGroupDOList)){
            return Maps.newHashMap();
        }
        List<ChatGroupBO> chatGroupBOList = ComponentLocator.getConverter().convertList(chatGroupDOList,ChatGroupBO.class);
        return chatGroupBOList.stream().collect(Collectors.toMap(ChatGroupBO::getId, x-> x));
    }

    @Override
    public ChatGroupBO queryById(Long chatGroupId) {
        ChatGroupDO chatGroupDO = chatGroupMapper.selectById(chatGroupId);
        return ComponentLocator.getConverter().convert(chatGroupDO,ChatGroupBO.class);
    }
}
